define(['core'], function (c) {
    var isDesktop = ('ondragstart' in window)
    var events
    if (isDesktop) {
        events = {
            moveStart: 'mousedown',
            onMove: 'mousemove',
            moveEnd: 'mouseup'
        }
    } else {
        events = {
            moveStart: 'touchstart',
            onMove: 'touchmove',
            moveEnd: 'touchend'
        }
    }

    function noop() { }
    var doc = c.doc
    var log = c.log
    var bindEvent = noop
    var unbindEvent = noop
    var leftBtn = 0
    if (doc.addEventListener) {
        bindEvent = function (obj, event, fn) {
            log('bindEvent: ' + event)
            obj.addEventListener(event, fn, false)
        }
        unbindEvent = function (obj, event, fn) {
            log('unbindEvent: ' + event)
            obj.removeEventListener(event, fn, false)
        }
    } else if (doc.attachEvent) {
        bindEvent = function (obj, event, fn) {
            log('bindEvent: ' + event)
            obj.attachEvent('on' + event, fn)
        }
        unbindEvent = function (obj, event, fn) {
            log('unbindEvent: ' + event)
            obj.detachEvent('on' + event, fn)
        }
    }

    function Dnd(element, options) {
        this.element = c.one(element)
        options = options || {}
        this.moveStart = options.moveStart || noop
        this.onMove = options.onMove || noop
        this.moveEnd = options.moveEnd || noop

        var el = this.element
        bindEvent(el, events.moveStart, this.start.bind(this))
        bindEvent(el, events.onMove, this.move.bind(this))
        bindEvent(el, events.moveEnd, this.end.bind(this))

        this.pushDown = false
        this.moving = false
        this.complete = false
        this.prevActionTime = 0
        this.pos = { x: 0, y: 0 }
    }

    Dnd.prototype.start = function (evt) {
        log('moveStart, button=' + evt.button)
        if (evt.button !== leftBtn) return
        this.pushDown = true
        this.prevActionTime = Date.now()
        this.pos = { x: evt.target.style.left * 1, y: evt.target.style.top * 1 }
        this.moveStart(evt)
    }

    Dnd.prototype.move = function (evt) {
        log('move')
        // if (!this.prevActionTime) return
        // this.moving = true
        // var now = Date.now()
        // if (now - this.prevActionTime > 500) return
        // this.prevActionTime = now
        this.onMove(evt)
    }

    Dnd.prototype.end = function (evt) {
        log('moveEnd')
        this.pushDown = false
        this.moving = false
        this.complete = true
    }

    return Dnd
})